using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using OpenComposite.EII.Repository;
using System.Xml.Schema;
using System.Xml;
using OpenComposite.Mapper;
using System.Xml.Serialization;
using System.IO;
using OpenComposite.EII.Forms;

namespace OpenComposite.EII.Designer.Workflow
{
	public partial class FormDataAggregatorConfiguration : Form
	{
		#region Initialization
		public FormDataAggregatorConfiguration(IServiceProvider serviceProvider)
		{
			_serviceProvider = serviceProvider;

			InitializeComponent();

			init();
		}

		private void init()
		{

			_bobject = null;

			showBObject();
		}

		#endregion

		#region Public Members

		#region Properties

		public Repository.BusinessObject BusinessObject
		{
			get { return _bobject; }
		}
		#endregion

		#region Methods

		public void Init(int outputBObjectId)
		{
			RepositoryService repSvc = (RepositoryService)this.GetService(typeof(RepositoryService));
			if ( repSvc == null )
				throw new NullReferenceException("RepositoryService not available.");

			// set business object
			if ( outputBObjectId > 0 ) {
				_bobject = repSvc.GetItem(ItemType.BObject, outputBObjectId, true) as Repository.BusinessObject;
				showBObject();
			} else {
				_bobject = null;
			}
		}

		#endregion

		#endregion

		#region Protected Members

		#region Methods
		protected override object GetService(Type service)
		{
			if ( _serviceProvider != null )
				return _serviceProvider.GetService(service);
			else
				return base.GetService(service);
		}
		#endregion

		#endregion

		#region Private Members

		#region Constants
		private const char cSeperator = ';';
		#endregion

		#region Variables
		private Repository.BusinessObject _bobject = null;

		private IServiceProvider _serviceProvider;

		#endregion

		#region Methods

		private void selectOutputBObject()
		{
			FormSelectRepositoryEx frm = new FormSelectRepositoryEx(_serviceProvider);
			frm.RepositoryID = Global.Status.CurrentRepositoryId;
			frm.DomainID = Global.Status.CurrentDomainId;
			frm.AllowedTypes = new ItemType[] { OpenComposite.EII.Repository.ItemType.BObject };
			frm.MultiSelect = false;
			frm.SelectedItem = _bobject;
			if ( frm.ShowDialog() == DialogResult.OK ) {
				_bobject = frm.SelectedItem as Repository.BusinessObject;
				showBObject();
			}
		}
		private void showBObject()
		{
			if ( _bobject == null ) {
				txtName.Text = "";
			} else {
				txtName.Text = _bobject.Name;
			}
		}
		private void showSchema(string sSchema)
		{
			if ( string.IsNullOrEmpty(sSchema) )
				return;

			XmlDocument xd = new XmlDocument();
			xd.LoadXml(sSchema);

			formXmlViewer frm = new formXmlViewer();
			frm.XmlDocument = xd;
			frm.ShowDialog();
		}

		#endregion

		#region Event Handler

		private void btnSelect_Click(object sender, EventArgs e)
		{
			selectOutputBObject();
		}
		private void btnView_Click(object sender, EventArgs e)
		{
			if ( _bobject != null )
				showSchema(_bobject.SchemaString);
		}
		
		private void tsbtnOK_Click(object sender, EventArgs e)
		{
			if ( _bobject == null ) {
				MessageBox.Show("Please select a business object.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
				return;
			}
			this.DialogResult = DialogResult.OK;
			this.Close();
		}
		private void tsbtnCancel_Click(object sender, EventArgs e)
		{
			this.DialogResult = DialogResult.Cancel;
			this.Close();
		}

		#endregion


		#endregion

	}
}